<?php
namespace app\admin\controller;

use think\Session;

class Role extends Rbac
{
//    角色管理页面
    public function role()
    {
//        当前页码
        if(isset($_GET['page'])){
            $page = $_GET['page'];
        }else{
            $page = '';
        }
        $_SESSION['pageTeam']=$page;
        $result=db("role")->paginate(10,false,['path'=>"/admin/role/role"]);
        $this->assign(["result"=>$result]);
        return $this->fetch('role');
    }
//    添加角色界面
    public function insert(){
        if(!empty($_POST)){
            $name=$_POST["r_name"];
            $data=[
                'r_name'=>$name,
                'f_id'=>$_POST['f_id']
            ];

            $erere=db("role")->where("r_name='$name'")->select();
//          判断用户是否存在
//          存在的话返回添加页面
            if($erere){
                $this->success("添加失败","admin/role/insert");
            }
//          不存在就插入
            else{
                $result=db("role")->insertGetId($data);
                if($result){
                    $p_id=implode(",",$_POST['p_id']);
                    $res1=$res2=db("role_perssion")->insert(['p_id'=>$p_id,'r_id'=>$result]);
                    if($res1){
                        $this->success("添加成功","admin/role/role");
                    }
                }else{
                    $this->error("添加失败");
                }

            }
        }
//        查询角色列表
        $result=db("role")->select();
        $this->assign(["result"=>$result]);

        return $this->fetch('insert');
    }
//    修改页面
    public function update(){
        if(!empty($_POST)){
//           表单提交数据
            $r_id=$_POST['r_id'];
//            原有数据
             $result1=db("role")
            ->alias("r")->join("role_perssion p","r.r_id=p.r_id")
                ->where("r.r_id='$r_id'")
                ->find();

            $data=[
                'r_name'=>empty($_POST['r_name'])?$result1['r_name']:$_POST['r_name'],
                'f_id'=>empty($_POST['f_id'])?$result1['f_id']:$_POST['f_id']
            ];
            $res1=db("role")->where("r_id=$r_id")->update($data);
            if($res1){
                $p_id=empty($_POST['p_id'])?$result1['p_id']:implode(",",$_POST['p_id']);
                $res2=db("role_perssion")->where("r_id=$r_id")->update(['p_id'=>$p_id]);
                if($res2){
                    $this->success("修改成功","admin/role/role");
                }
            }
        }

//        查询角色列表
        $result=db("role")->select();
//        查询总条数
        $count=db("role")->field("count(*)")->find();


//        根据id实现回显
        $id=request()->param("id");
        $result1=db("role")
            ->alias("r")->join("role_perssion p","r.r_id=p.r_id")
            ->where("r.r_id='$id'")
            ->find();
//        传参
        $this->assign(["result"=>$result,"count"=>$count,"result1"=>$result1]);

        return $this->fetch("update");

    }
//    根据名字搜索
    public function namep(){
        $name=request()->param('name');
        if(!empty($name)){
            $result=db("role")->where("r_name like '%$name%'")->paginate(10,false,['path'=>"/crm/public/index.php/admin/role/role"]);
        }else{
            $result=db("role")->paginate(10,false,['path'=>"/crm/public/index.php/admin/role/role"]);
        }
        $this->assign(["result"=>$result]);
        return $this->fetch('role');
    }
}
